package ua.org.nuos.sdms.clientgui.client.handlers;

import ua.org.nuos.sdms.clientgui.client.context.Messages;
import ua.org.nuos.sdms.clientgui.client.controller.GlobalBoard;
import ua.org.nuos.sdms.clientgui.client.events.ChangeCurrentGroupEvent;
import ua.org.nuos.sdms.clientgui.client.events.CreateNewGroupEvent;
import ua.org.nuos.sdms.clientgui.client.events.ErrorMessageEvent;
import ua.org.nuos.sdms.clientgui.client.listeners.CreateNewGroupListener;
import ua.org.nuos.sdms.clientgui.server.AppProxy;
import ua.org.nuos.sdms.middle.entity.Group;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 04.03.12
 * Time: 17:31
 * To change this template use File | Settings | File Templates.
 */
public class CreateNewGroupHandler implements CreateNewGroupListener {
    @Override
    public void create(CreateNewGroupEvent event) {
        Group group = event.getGroup();
        try {
            Group g = AppProxy.getInstance().getServiceLocator().getClientBean().createGroup(group);
            AppProxy.getInstance().getController(GlobalBoard.ID).fire(new ChangeCurrentGroupEvent(g));
        } catch (Exception e) {
            Logger.getAnonymousLogger().log(Level.WARNING, "Cant create new group", e);
            AppProxy.getInstance().getController(GlobalBoard.ID).fire(new ErrorMessageEvent(Messages.ACTION_ERROR));
        }
    }
}
